var map,
    markersArray = [],
    redMarkersCount = 0;

function toHHMMSS(seconds) {
    "use strict";
    var hours   = Math.floor(seconds / 3600),
        minutes = Math.floor((seconds - (hours * 3600)) / 60);
    seconds = seconds - (hours * 3600) - (minutes * 60),
    time;

    if (hours   < 10) {hours   = "0"+hours;}
    if (minutes < 10) {minutes = "0"+minutes;}
    if (seconds < 10) {seconds = "0"+seconds;}
    var time    = hours+':'+minutes+':'+seconds;
    return time;
}

function addMarker(carStop, carStopId) {
    "use strict";
    carStopId = parseInt(carStopId, 10);
    var color = carStop.status === 1 ? color1: color2,
        scale = carStop.length / maxStopLength *  (maxSymbolRadius - minSymbolRadius) / 5  + 5,
        position = new google.maps.LatLng(carStop.latitude, carStop.longitude),
        icon = {
            path: google.maps.SymbolPath.CIRCLE,
            scale: scale,
            fillColor: color,
            fillOpacity: opacity,
            strokeColor: color,
            strokeOpacity: opacity
        },
        marker = carStop.status === 1 ? new google.maps.Marker({
            position: position,
            icon: icon,
            map: map
        }) :
        new MarkerWithLabel({
            position: position,
            icon: icon,
            map: map,
            labelContent: ++redMarkersCount,
            labelAnchor: new google.maps.Point(15, 0),
            labelClass: "labels",
            labelStyle: {opacity: opacity, color: "black", "font-size": "10px", "text-align": "center",
                         "background-color": "white", "font-weight": "bold", width: "30px"}
        }),
        info = new google.maps.InfoWindow({
            content: carStopId + 1 + ". " + carStop.time + " Stop " + toHHMMSS(carStop.length)
        });
        if (carStop.status === 0) marker.setZIndex(google.maps.Marker.MAX_ZINDEX + 1);
    google.maps.event.addListener(marker, "click", function (e) { info.open(map, this); });
    markersArray.push(marker);
}

/**
 * Shows any overlays currently in the array
 */
function showOverlays() {
    "use strict";
    var i;
    if (markersArray) {
        for (i in markersArray) {
            if (markersArray.hasOwnProperty(i)) {
                markersArray[i].setMap(map);
            }
        }
    }
}

/**
 * Removes the overlays from the map, but keeps them in the array
 */
function clearOverlays() {
    "use strict";
    var i;
    if (markersArray) {
        for (i in markersArray) {
            if (markersArray.hasOwnProperty(i)) {
                markersArray[i].setMap(null);
            }
        }
    }
}

function initialize() {
    "use strict";
    var carStopId;
    for (carStopId in carStops) {
        if (carStops.hasOwnProperty(carStopId)) {
            addMarker(carStops[carStopId], carStopId);
        }
    }
    var mapOptions = {
        center: new google.maps.LatLng(carStops[0].latitude, carStops[0].longitude),
        zoom: 12,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById("map_canvas"),
                                    mapOptions);
    showOverlays();
}


window.onload = initialize;
